/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.MemberContentUploadBean;
import ken.app.bean.MemberGoodBean;
import ken.app.bean.MemberModifyNameBean;
import ken.app.bean.TreeAllBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class MemberModifyNameXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_content = false;
	private boolean in_result = false;
	private boolean in_message = false;
	private boolean in_error_code = false;
	
	private List<TreeAllBean> list;
//	private TreeAllBean mTreeInfo;
	private MemberModifyNameBean memberModifyNameBean;
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public MemberModifyNameBean getParsedData() {
		return memberModifyNameBean;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {

	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
		if (localName.equals("content")) {
			this.in_content = true;
			/* 解析到item的開頭時new一個News物件 */
			memberModifyNameBean = new MemberModifyNameBean();
//			Log.d("ken", buf.toString());
		} else if (localName.equals("result")) {
			if (this.in_content) {
				this.in_result = true;
			} 
		} else if (localName.equals("message")) {
			if (this.in_content) {
				this.in_message = true;
			}
		} else if (localName.equals("error_code")) {
			if (this.in_content) {
				this.in_error_code = true;
			}
		}
		
		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		
		if (localName.equals("content")) {
			this.in_content = false;
			/* 解析到item的結尾時將News物件寫入List中 */

		} else if (localName.equals("result")) {
			if (this.in_content) {
				/* 設定News物件的title */
				memberModifyNameBean.setResult(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_result = false;
			}
		} else if (localName.equals("message")) {
			if (this.in_content) {
				/* 設定News物件的link */
				memberModifyNameBean.setMessage(buf.toString().trim());
				buf.setLength(0);
				this.in_message = false;
			}
		} else if (localName.equals("error_code")) {
			if (in_content) {
				memberModifyNameBean.setErrorCode(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_error_code = false;
			}
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_content || this.in_error_code) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}